package DAOMysql;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import model.AccessDTO;

import DAO.AccessDAO;
import DAO.NotFoundException;

public class AccessDAOImpl implements AccessDAO{
	
	private static Connection getConnection(){
		try{
			Class.forName("com.mysql.jdbc.Driver");
		}
		catch (ClassNotFoundException e1) {
			e1.printStackTrace();
		}
		Connection conn = null;
		try{
			conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/fbsdatabase",
					"root", "password");
			
		}
		catch(SQLException e){
			e.printStackTrace();
		}
		return conn;

	}
	
	public AccessDTO findByPageName(String pgname) throws NotFoundException{
		String selectSql = "SELECT * FROM access WHERE PageName='"+pgname.trim()+"';";
		AccessDTO accessThis = new AccessDTO();
		
		Connection conn = getConnection();
		
		try{
			Statement stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery(selectSql);
			while(rs.next()){
				accessThis.setPageID(rs.getString("PageID"));
				accessThis.setPageName(rs.getString("PageName"));
				accessThis.setManager(rs.getString("Manager"));
				accessThis.setAdmin(rs.getString("Admin"));
				accessThis.setStaff(rs.getString("Staff"));
			}
			stmt.close();
		}
		catch(Exception e){
			e.printStackTrace();
		}
		finally{
			try{
				conn.close();
			}
			catch(Exception e){
				e.printStackTrace();
			}
			
		}
		return accessThis;
	}
}